package com.example.anew.journey.mynewjourney.firstchangelogic.personalInformation;

import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.FirstChangeAllBaseActivity;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.personalInformation.persenter.FirstChangePersonalInformationPersenter;
import com.example.anew.journey.mynewjourney.firstchangelogic.personalInformation.view.FirstChangePersonalInformationView;
import com.example.anew.journey.mynewjourney.tool.GlideCircleTransform;
import com.example.anew.journey.mynewjourney.utils.GlideImageLoader;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.view.CropImageView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 杭州惟君医疗科技有限公司16
 * Created by 20433 on 2018/3/15
 */

public class FirstChangePersonalInformationActivity extends FirstChangeAllBaseActivity implements View.OnClickListener,FirstChangePersonalInformationView {

    @Bind(R.id.activity_first_change_personal_information_tv_head_portrait)
    TextView activityFirstChangePersonalInformationTvHeadPortrait;
    @Bind(R.id.activity_first_change_personal_information_iv_setting_head_portrait)
    ImageView activityFirstChangePersonalInformationIvSettingHeadPortrait;
    @Bind(R.id.activity_first_change_personal_information_iv_head_portrait_next_stop)
    ImageView activityFirstChangePersonalInformationIvHeadPortraitNextStop;
    @Bind(R.id.activity_first_change_personal_information_rl_head_portrait)
    RelativeLayout activityFirstChangePersonalInformationRlHeadPortrait;
    @Bind(R.id.activity_first_change_personal_information_tv_nickname)
    TextView activityFirstChangePersonalInformationTvNickname;
    @Bind(R.id.activity_first_change_personal_information_iv_nickname_next_stop)
    ImageView activityFirstChangePersonalInformationIvNicknameNextStop;
    @Bind(R.id.activity_first_change_personal_information_tv_nickname_setting)
    TextView activityFirstChangePersonalInformationTvNicknameSetting;
    @Bind(R.id.activity_first_change_personal_information_rl_nickname)
    RelativeLayout activityFirstChangePersonalInformationRlNickname;
    @Bind(R.id.activity_first_change_personal_information_tv_birthday)
    TextView activityFirstChangePersonalInformationTvBirthday;
    @Bind(R.id.activity_first_change_personal_information_iv_birthday_next_stop)
    ImageView activityFirstChangePersonalInformationIvBirthdayNextStop;
    @Bind(R.id.activity_first_change_personal_information_tv_birthday_setting)
    TextView activityFirstChangePersonalInformationTvBirthdaySetting;
    @Bind(R.id.activity_first_change_personal_information_rl_birthday)
    RelativeLayout activityFirstChangePersonalInformationRlBirthday;
    private View view;
    private FirstChangePersonalInformationPersenter mFirstChangePersonalInformationPersenter = new FirstChangePersonalInformationPersenter(this);
    private SimpleDateFormat time;
    private TextView tv_taking_pictures;
    private TextView tv_choose_from_your_phone_album;
    private TextView tv_cancel;
    private ImageView iv_delect;
    private TextView tv_save;
    private EditText et_input_name;
    ArrayList<ImageItem> images = null;
    private ImagePicker imagePicker;

    /**
     * @desc 主要内容的布局资源填充进来
     */
    @Override
    protected View setMainContentView() {
        view = View.inflate(this, R.layout.activity_first_change_personal_information, null);
         ButterKnife.bind(this, view);
        return view;
    }

    /**
     * @desc 初始化数据 onCreate()
     */
    @Override
    protected void initView(View view) {
        displayToolbar();
        displayReturn();
        displayTvName();

        setToolbarBg(0xff2196F3);
        setReturnrePictuResources(R.mipmap.return_02);
        setTvNameEachAttribute(20, 0xffffffff, "个人信息");

        initDate();

    }

    private void initDate() {
        activityFirstChangePersonalInformationRlHeadPortrait.setOnClickListener(this);
        activityFirstChangePersonalInformationRlNickname.setOnClickListener(this);
        activityFirstChangePersonalInformationRlBirthday.setOnClickListener(this);
        time = new SimpleDateFormat("yyyy年MM月dd日");
        mFirstChangePersonalInformationPersenter.initInformation(this);
       String birthday =  mFirstChangePersonalInformationPersenter.ForBirthday(this);
        activityFirstChangePersonalInformationTvBirthdaySetting.setText(birthday);

        // todo 获取的 ImagePicker 对象
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader()); //TODO 设置图片加载器
        imagePicker.setMultiMode(false);  //TODO 多选
        imagePicker.setShowCamera(true);  //TODO 显示相机 显示拍照按钮
        imagePicker.setSaveRectangle(false); //TODO 是否按矩形区域保存图片

        imagePicker.setStyle(CropImageView.Style.CIRCLE);
        Integer radius = Integer.valueOf(180);
        radius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radius, getResources().getDisplayMetrics());
        imagePicker.setFocusWidth(radius * 2);
        imagePicker.setFocusHeight(radius * 2);
        // TODO 设置图片保存宽高 保存文件的宽高
        imagePicker.setOutPutX(800);
        imagePicker.setOutPutY(800);

    }


    @Override
    protected void changeInterface() {
        super.changeInterface();

        mFirstChangePersonalInformationPersenter.getPersonalInformation(this);
    }

    /**
     * @desc Activity 销毁 释放资源
     */
    @Override
    protected void releaseResources() {
        ButterKnife.unbind(view);
    }

    /**
     * @desc 网络监听 用于提示用户有网络了
     */
    @Override
    protected void promptHaveInternet() {

    }

    /**
     * @desc 网络监听 用于提示用户没有网络了
     */
    @Override
    protected void promptNoInternet() {

    }

    /**
     * @desc 点击分享按钮
     */
    @Override
    protected void onClickShared() {

    }

    /**
     * @desc 点击发布按钮
     */
    @Override
    protected void onClickRelease() {

    }

    /**
     * @desc 点击返回按钮
     */
    @Override
    protected void onClickReturn() {
        finish();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_first_change_personal_information_rl_head_portrait:   //头像
                mFirstChangePersonalInformationPersenter.onClickeHeadPortrait(this);
                break;
            case R.id.activity_first_change_personal_information_rl_nickname:  //昵称
                mFirstChangePersonalInformationPersenter.onClickeNickname(this);
                break;
            case R.id.activity_first_change_personal_information_rl_birthday: //生日
                mFirstChangePersonalInformationPersenter.onClickeBirthday(this);
                break;
            case R.id.personal_information_nickname_popu_iv_delect:   //点击昵称选框的 取消按钮
                mFirstChangePersonalInformationPersenter.onClickeNicknameOptionsCancel();
                break;
            case R.id.personal_information_nickname_popu_tv_Save:   //点击昵称选框的 保存按钮
                mFirstChangePersonalInformationPersenter.onClickeNicknameOptionsSave(this);
                break;
            case R.id.personal_information_head_portrait_popu_tv_taking_pictures:  //点击头像选框的 拍照按钮
                mFirstChangePersonalInformationPersenter.onClickeHeadPortraitOptionTakingPictures();
                break;
            case R.id.personal_information_head_portrait_popu_tv_choose_from_your_phone_album:    //点击头像选框的 从手机相册选择按钮
                mFirstChangePersonalInformationPersenter.onClickeHeadPortraitOptionChooseFromYourPhoneAlbum();
                break;
            case R.id.personal_information_head_portrait_popu_tv_cancel:     //点击头像选框的 取消按钮
                mFirstChangePersonalInformationPersenter.onClickeHeadPortraitOptionsCancel();
                break;
        }
    }


    // 显示提示Toast信息
    @Override
    public void showToastInformation(String str) {
        Toast.makeText(this, str,Toast.LENGTH_SHORT).show();
    }
    // 头像选项取消
    @Override
    public void headOptionsCancel() {

    }

    //昵称取消
    @Override
    public void nicknameCancel() {

    }
    //昵称保存
    @Override
    public void nicknameSave() {

    }
    //获取昵称
    @Override
    public String getNickname() {
        return null;
    }
    //设置昵称
    @Override
    public void setNickname(String str) {
        activityFirstChangePersonalInformationTvNicknameSetting.setText(str);
    }
    //获取生日
    @Override
    public String getBirthday() {
        return null;
    }
    //设置生日
    @Override
    public void setBirthday(Date date) {
        activityFirstChangePersonalInformationTvBirthdaySetting.setText(time.format(date));
    }
    //获取输入的昵称
    @Override
    public String getInputNickname() {
        String str = et_input_name.getText().toString().trim();
        if(str == null && TextUtils.isEmpty(str)){
            return "";
        }
        return str;
    }
    //设置昵称
    @Override
    public void setInputNickname(String str) {

    }
    //取消昵称弹框
    @Override
    public void NicknameBouncedCancel() {

    }
    // 获取昵称弹框的控件
    @Override
    public void findInputNicknameView(View view) {
        iv_delect = view.findViewById(R.id.personal_information_nickname_popu_iv_delect);
        tv_save = view.findViewById(R.id.personal_information_nickname_popu_tv_Save);
        et_input_name = view.findViewById(R.id.personal_information_nickname_popu_et_input_name);

        iv_delect.setOnClickListener(this);
        tv_save.setOnClickListener(this);
    }
    //获取头像
    @Override
    public String getHeadPortrait() {
        return null;
    }
    //设置头像
    @Override
    public void setHeadPortrait(String str) {
        String URL = RBConstants.SERVER_URL +"/Public/Uploads/"+str;
           Glide
                .with(this)
                .load(URL)
                .transform(new BitmapTransformation[]{new GlideCircleTransform(this)})
                .into(activityFirstChangePersonalInformationIvHeadPortraitNextStop);

    }

    @Override
    public void setBirthdayOne(String str) {
        activityFirstChangePersonalInformationTvBirthdaySetting.setText(str);
    }
    //弹出头像选择
    @Override
    public void toImageSelection() {
        //TODO 开启相册
        Intent intent = new Intent(this, ImageGridActivity.class);
        intent.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
        startActivityForResult(intent, 1);
    }

    // 获取头像弹框的控件
    @Override
    public void findHeadOptionsView(View view) {
        tv_taking_pictures = view.findViewById(R.id.personal_information_head_portrait_popu_tv_taking_pictures);
        tv_choose_from_your_phone_album = view.findViewById(R.id.personal_information_head_portrait_popu_tv_choose_from_your_phone_album);
        tv_cancel = view.findViewById(R.id.personal_information_head_portrait_popu_tv_cancel);

        tv_taking_pictures.setOnClickListener(this);
        tv_choose_from_your_phone_album.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            if (data != null && requestCode == 1) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                String URL = images.get(0).path;
              //  NLogger.e("TAG","============submitHeadPortraitResources===URL=============="+URL);
                imagePicker.getImageLoader().displayImage( this, images.get(0).path, activityFirstChangePersonalInformationIvHeadPortraitNextStop, 160, 150);
                mFirstChangePersonalInformationPersenter.submitHeadPortraitResources(images.get(0).path,this);
            } else {
                Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    }

}
